package com.ruoyi.web.controller.system;

import com.ruoyi.email.entity.FEmailEntity;
import com.ruoyi.email.service.EmailService;
import com.ruoyi.system.domain.SegRegEmailInfo;
import com.ruoyi.system.service.ISegRegEmailInfoService;
import com.ruoyi.system.service.ISegUploadDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.RegisterBody;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.web.service.SysRegisterService;
import com.ruoyi.system.service.ISysConfigService;
import com.ruoyi.common.core.redis.RedisCache;

import javax.annotation.Resource;
import java.util.Random;

/**
 * 注册验证
 *
 * @author ruoyi
 */
@RestController
public class SysRegisterController extends BaseController
{
    @Autowired
    private SysRegisterService registerService;

    @Autowired
    private ISysConfigService configService;

    @Autowired
    private RedisCache redisCache;

    @Resource
    EmailService emailService;

    @Value(value = "${server.port}")
    public String port;

    @Value(value = "${server.ip}")
    public String ip;

    @Autowired
    public ISegRegEmailInfoService segRegEmailInfoService;

    @Autowired
    public ISegUploadDetailService segUploadDetailService;


    @PostMapping("/register")
    public AjaxResult register(@RequestBody RegisterBody user)
    {
        // 查看是否注册但是未激活
        String msg = registerService.checkAccount(user);

        // 注册
        if(StringUtils.isEmpty(msg)){
            msg = registerService.register(user);
        }

        // 发送邮件
        if(StringUtils.isEmpty(msg)){
            // 记录code到数据库
            String code = emailPassword();
            SegRegEmailInfo info = new SegRegEmailInfo();
            info.setUserId(segUploadDetailService.userInfoConvert(user.getUsername(), true).user_id);
            info.setCode(code); info.setType("0"); // 0是注册
            segRegEmailInfoService.insertSegRegEmailInfo(info);

            // 邮件发送
            FEmailEntity fEmailEntity = new FEmailEntity();
            fEmailEntity.setSendTo(user.getEmail());
            String net = "http://" + ip + ":" + port + "/system/email/emailActivate/" + user.getUsername() + "_" + code;
            String Hyperlink = String.format("<a href='%s'>%s</a>", net, net);
//            String Hyperlink = net;
            String FDetail = "您好！\n请通过下方链接激活账号。\n若非本人操作，可忽略此邮件。\n";
            fEmailEntity.setDetail(FDetail + Hyperlink);
            fEmailEntity.setTitle("激活您的ARCUS账号");

            emailService.sendSimpleMail(fEmailEntity);
            return success("登录账号时将会有一份激活邮件发送至您的邮箱");
        }
        return error(msg);
    }

    public String emailPassword() {
        StringBuilder flag = new StringBuilder();
        String sources = "0123456789";// 加上一些字母，就可以生成pc站的验证码了
        Random rand = new Random();
        for (int j = 0; j < 6; j++) {
            flag.append(sources.charAt(rand.nextInt(9)));
        }
        return flag.toString();
    }
}
